package com.chang.search;

import com.chang.common.exception.BizCodeEnum;
import com.chang.common.to.SkuEsModel;
import com.chang.common.utils.R;
import com.chang.search.service.ProductSaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/*
 * @Author Changzhenxi
 * @Date 2024/6/4 9:29
 * @Description This is description of method
 */
@Slf4j
@RestController
@RequestMapping("search/save")
public class ElasticSaveController {

    // 构造器注入
    private final ProductSaveService productSaveService;

    public ElasticSaveController(ProductSaveService productSaveService) {
        this.productSaveService = productSaveService;
    }

    // 上架商品
    @PostMapping("product")
    public R productStatusUp(@RequestBody List<SkuEsModel> skuEsModels) throws IOException {
        Boolean finalStatus;
        try {
            finalStatus = productSaveService.productStatusUp(skuEsModels);
        } catch (Exception e) {
            log.error("ElasticSaveController商品上架错误:{}" + e.getMessage());
            return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnum.PRODUCT_UP_EXCEPTION.getMsg());
        }
        if (finalStatus) return R.ok();
        else return R.error();
    }
}
